package com.example.administrator.common.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yunniuusedcar.R;

//import com.ithbbma.takeout.R;
//import com.ithbbma.takeout.mvps.IBaseView;

/**
 * Activity基类，所有的Activity都需要继承此类。
 * 封装： 查看子控件，设置监听器，初始化数据，
 * toast, showDialog, showProgressDialog等方法
 * Created by Administrator on 2017/11/28.
 * @author LQW
 */
public abstract class BaseActivity extends AppCompatActivity
        implements IUIOperation {  //, IBaseView
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutRes());

        initView();
        initListener();
        initData();

    }


    /** 查找子控件，可以省略强转 */
    public <T> T findView(int id) {
        T view = (T) findViewById(id);
        return view;
    }

    public void showToast(String text) {
        Global.showToast(text);
    }

    private ProgressDialog mPDialog;

    /**
     * 显示加载提示框(不能在子线程调用)
     */
    protected void showProgressDialog(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPDialog = new ProgressDialog(BaseActivity.this);
                mPDialog.setMessage(message);
                // 点击外部时不销毁弹出窗
                mPDialog.setCanceledOnTouchOutside(false);

                // activity如果正在销毁或已销毁，不能show Dialog，否则出错。
                if (!isFinishing())
                    mPDialog.show();
            }
        });
    }


    protected void setPageTitle(String title) {
        TextView tvTitle = findView(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    /**
     * 销毁加载提示框
     */
    protected void dismissProgressDialog() {
        if (mPDialog != null) {
            mPDialog.dismiss();
            mPDialog = null;
        }
    }

    /**
     * 显示对话框
     *
     * @param title 标题
     * @param message 内容
     * @param listener 回调监听器
     */
    protected void showDialog(String title, String message,
                              final OnDialogClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            listener.onConfirm(dialog);
                        }
                    }
                });
        builder.setNegativeButton("取消", null);
        if (!isFinishing())
            builder.create().show();
    }

    /** 提示对话框*/
    protected void showDialog(String title, String message) {
        showDialog(title, message, null);
    }

    /** 提示对话框*/
    protected void showDialog(String message) {
        showDialog("", message, null);
    }

    /** 对话框点击回调 */
    public interface OnDialogClickListener {

        /** 确定 */
        void onConfirm(DialogInterface dialog);

        /** 取消 */
        void onCancel(DialogInterface dialog) ;
    }

//    @Override    //重写网络请求  成功的方法
//    public void onHttpSuccess(int reqType, Message msg) {
//    }
//
//    @Override
//    public void onHttpError(int reqType, String error) {
//        showDialog(error);		// 提示失败信息
//    }
}

